// pages/classify/classify.js
var util = require('../../utils/util')   //引入配置公共请求接口
const config = require('../../config/config');
const app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    footprintGuid: '', //足迹全局id
    partnerInfo:{},
    isShowPartnerAll:true,
    CustomBar: app.globalData.CustomBar,
    statusHeight:app.globalData.StatusBar,
    active: 1,
    classList: [],
    classId: 0,
  },
    // 展示合伙人二维码
    partnerCode: function(e) {
      const codeImg = e.target.dataset.url
      wx.previewImage({
        urls: [codeImg],
        current: codeImg // 当前显示图片的http链接
      })
    },
    // 关闭合伙人二维码
    closeCodePop: function() {
      this.setData({
        showPartnerCodePop: false
      })
    },
    // 拨打合伙人电话
    partnerPhone: function() {
      const that = this
      wx.makePhoneCall({
        phoneNumber: that.data.partnerInfo.partnerPhone
      })
    },
   // 展开合伙人信息
   showPartnerAll: function() {
    this.setData({
      isShowPartnerAll:!this.data.isShowPartnerAll
    })
  },
  goHome(e) {
    wx.redirectTo({
      url: '/pages/newIndex/newIndex',
    })
  },
  gotoCarbuy(e){   //进入购物车
    wx.reLaunch({
      url: '/packagBuy/pages/carbuy/carbuy',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#000000'
    })
    this.setData({
      isIPXBottom:app.globalData.isIPXBottom?app.globalData.isIPXBottom:'0rpx'
    })
    this.getClassify()
    let classifyInitTimer = setInterval(() => {
      if (this.data.classList.length > 0) {
        clearInterval(classifyInitTimer)
        this.setData({
          classId: this.data.classList[0].id,
          statusHeight:app.globalData.StatusBar
        })
        this.setData({
          classChild:this.data.classList[0].children,
          headerImg:this.data.classList[0],
        })   
      }
    }, 100)
  },
  goreBack(e){
    wx.navigateBack({
      delta: 1,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      footprintGuid: util.guid()
    })
    setTimeout(()=>{
      this.setData({
        partnerInfo:getApp().globalData.partnerInfo
      })
      },500)
    //保存访问足迹
    app.saveAccessFootprint(this.data.footprintGuid, 'classify', null)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    //保存退出足迹
    app.saveQuitFootprint(this.data.footprintGuid)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.refresh()
  },
  async refresh(){
    this.getClassify()
   
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 禁用用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // },
  goreBack: function (e) {
    wx.navigateBack({
      delta: 1
    })
  },
    // 拨打合伙人电话
    partnerPhone: function() {
      const that = this
      wx.makePhoneCall({
        phoneNumber: that.data.partnerInfo.partnerPhone
      })
    },
  getClassify:function(e){  //获取商品类目
    var that=this
    app.reqFun.requestJson('GET',config.classifyData,' ',(res)=>{
      if(res.data.success==1){
        that.setData({
          classList:res.data.data,
          classChild:res.data.data[0].children,
          headerImg:res.data.data[0].icon
        })
      }else{
        wx.showModal({
          title: '网络错误',
          content: '网络出错，请刷新重试',
          showCancel: false
        })  
      }   
    })
  },
    // 切换分类
    changeClass(e) {
      var that=this;
      var id=e.currentTarget.dataset.id
      var classfiy=that.data.classList
      for(let val of classfiy){
        if(val.id==id){
          that.setData({
            classChild:val.children,
            headerImg:val,
          })       
        }
      }
    }, 
    classSearch(e){  //点击类目进入查询
      var info=e.currentTarget.dataset.val
      if(info.relyType=='category'){
        wx.navigateTo({
          url: '/packageShop/pages/shopSearch/shopSearch?classifyId='+info.relyValue+'&searchType=class&className='+info.name+'&type=c',
        })
      }else if(info.relyType=='link'){   //参数路径必须是
        wx.navigateTo({
          url: `../discovery/discovery?url=${info.relyValue}`,
        })
      }else if(info.relyType=='page'){
        wx.navigateTo({
          url: info.relyValue,
        })
      }
     
    },
    goSearchPage(e){   //商品搜索进入商品列表
      wx.navigateTo({
        url: '/packageShop/pages/shopSearch/shopSearch?type=s',
      })       
    },
})

// cat: "2031"
// provinceCode: "510000000000"
// sort: "sort_random"